package cn.xscrum.framework.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.system.domain.SysAreas;
import cn.xscrum.system.service.ISysAreasService;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.utils.poi.ExcelUtil;
import cn.xscrum.common.utils.StringUtils;
import cn.xscrum.common.core.domain.Ztree;

/**
 * 区域信息Controller
 * 
 * @author xscrum
 * @date 2020-01-05
 */
@Controller
@RequestMapping("/system/areas")
public class SysAreasController extends BaseController
{
    private String prefix = "system/areas";

    @Autowired
    private ISysAreasService sysAreasService;

    @RequiresPermissions("system:areas:view")
    @GetMapping()
    public String areas()
    {
        return prefix + "/areas";
    }

    /**
	 * 查询区域信息树列表
     */
    @RequiresPermissions("system:areas:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysAreas> list(SysAreas sysAreas)
    {
        List<SysAreas> list = sysAreasService.selectSysAreasList(sysAreas);
        return list;
    }

    /**
	 * 导出区域信息列表
     */
    @RequiresPermissions("system:areas:export")
    @Log(title = "区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAreas sysAreas)
    {
        List<SysAreas> list = sysAreasService.selectSysAreasList(sysAreas);
        ExcelUtil<SysAreas> util = new ExcelUtil<SysAreas>(SysAreas.class);
        return util.exportExcel(list, "areas");
    }

    /**
	 * 新增区域信息
     */
    @GetMapping(value = { "/add/{areaId}", "/add/" })
    public String add(@PathVariable(value = "areaId", required = false) Long areaId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(areaId))
        {
            mmap.put("sysAreas", sysAreasService.selectSysAreasById(areaId));
        }
        return prefix + "/add";
    }

    /**
	 * 新增保存区域信息
     */
    @RequiresPermissions("system:areas:add")
    @Log(title = "区域信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAreas sysAreas)
    {
        return toAjax(sysAreasService.insertSysAreas(sysAreas));
    }

    /**
	 * 修改区域信息
     */
    @GetMapping("/edit/{areaId}")
    public String edit(@PathVariable("areaId") Long areaId, ModelMap mmap)
    {
        SysAreas sysAreas = sysAreasService.selectSysAreasById(areaId);
        mmap.put("sysAreas", sysAreas);
        return prefix + "/edit";
    }

    /**
	 * 修改保存区域信息
     */
    @RequiresPermissions("system:areas:edit")
    @Log(title = "区域信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAreas sysAreas)
    {
        return toAjax(sysAreasService.updateSysAreas(sysAreas));
    }

    /**
	 * 删除
     */
    @RequiresPermissions("system:areas:remove")
    @Log(title = "区域信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{areaId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("areaId") Long areaId)
    {
        return toAjax(sysAreasService.deleteSysAreasById(areaId));
    }

    /**
	 * 选择区域信息树
     */
    @GetMapping(value = { "/selectAreasTree/{areaId}", "/selectAreasTree/" })
    public String selectAreasTree(@PathVariable(value = "areaId", required = false) Long areaId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(areaId))
        {
            mmap.put("sysAreas", sysAreasService.selectSysAreasById(areaId));
        }
        return prefix + "/tree";
    }

    /**
	 * 加载区域信息树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = sysAreasService.selectSysAreasTree();
        return ztrees;
    }
}